package cn.com.shopec.erp.product.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.shopec.erp.base.entity.SupplierInfo;
import cn.com.shopec.erp.common.constant.SZSCMConstant;
import cn.com.shopec.erp.common.utils.JsonHelper2;
import cn.com.shopec.erp.common.utils.LogicUtil;
import cn.com.shopec.erp.common.utils.StringUtil;
import cn.com.shopec.erp.product.entity.CommodityCat;
import cn.com.shopec.erp.product.service.IBrandService;
import cn.com.shopec.erp.product.service.ICatBrandService;
import cn.com.shopec.erp.product.service.ICommodityCatService;
import cn.com.shopec.erp.product.vo.BrandVo;
import cn.com.shopec.erp.product.vo.CommodityCatVo;

/**
 * @ClassName: CommodityCatController
 * @Description: 商品分类controller
 * @author: gaoyaxiong
 * @date: 2013-5-30 下午14:14:13
 */
@Controller
@RequestMapping("product/cat")
public class CommodityCatController {
	private static Log log = LogFactory.getLog(CommodityCatController.class);
	@Resource
	private ICommodityCatService commodityCatService;
	
	@Resource
	private IBrandService brandService;
	
	@Resource
	private ICatBrandService catBrandService;
	
	/**
     * @Title: getCommodityCatList
     * @Description: 跳转到分类列表页面
     * @param catVo
     * @param map
     * @return
     * @author: gaoyaxiong
     * @date: 2013-5-27下午01:31:46
     */
	@RequestMapping("getCommodityCatList")
	public String getCommodityCatList(CommodityCatVo catVo,ModelMap map){
		map.put("catVo", catVo);
		return "product/commodity_cat_list";
	}

	
	/**
     * @Title: addToCommodityCat
     * @Description: 跳转到新增分类页面
     * @param catVo
     * @param map
     * @return
     * @author: gaoyaxiong
     * @date: 2013-5-27下午01:31:46
     */
	@RequestMapping("addToCommodityCat")
	public String addToCommodityCat(ModelMap map){
		//查询所有品牌
//		List<BrandVo> brandVoList = null;
//		try {
//			brandVoList = brandService.getBrandList();
//		} catch (Exception e) {
//			log.error("addToCommodityCat "+e.getMessage(), e);
//		}
//		map.put("brandVoList", brandVoList);
		return "product/add_commodity_cat";
	}
	
	/**
     * @Title: getAjaxNextCommodityCat
     * @Description: 获取分类
     * @param catVo
     * @param map
     * @return
     * @author: gaoyaxiong
     * @date: 2013-5-27下午01:31:46
     */
	@RequestMapping("getAjaxNextCommodityCat")
	@ResponseBody
	public String getAjaxNextCommodityCat(CommodityCatVo catVo,HttpServletResponse response){
		List<CommodityCatVo> list = commodityCatService.getCommodityCatList(catVo);
		if(list!=null && list.size()>0){
	    	response.setContentType("text/html;charset=utf-8");
	    	try {
				response.getWriter().append(JsonHelper2.toJson(list));
			} catch (IOException e) {
				log.error("getAjaxNextCommodityCat "+e.getMessage(), e);
			}
	      return null;
	    }
		return "";
	}
	
	/**
     * @Title: addCommodityCat
     * @Description: 新增分类
     * @param catVo
     * @param map
     * @return
     * @author: gaoyaxiong
     * @date: 2013-5-27下午01:31:46
     */
	@RequestMapping("addCommodityCat")
	@ResponseBody
	public String addCommodityCat(CommodityCatVo catVo,String[] brandNO,ModelMap map){		
		return commodityCatService.addCommodityCat(catVo, brandNO);
	}
	
	
	/**
     * @Title: updateTOCommodityCat
     * @Description: 跳转到分类修改页面
     * @param catVo
     * @param map
     * @return
     * @author: gaoyaxiong
     * @date: 2013-5-27下午01:31:46
     */
	@RequestMapping("updateToCommodityCat")
	public String updateTOCommodityCat(CommodityCatVo catVo,ModelMap map) {
		List<CommodityCatVo> catlist = new ArrayList<CommodityCatVo>();
		if(StringUtil.isExist(catVo.getCatNo())){
			catlist = commodityCatService.getCommodityCatList(catVo);
		}
//		List<BrandVo> brandVoList = null;
//		try {
//			brandVoList = brandService.getBrandList();
//		} catch (Exception e) {
//			log.error("updateToCommodityCat "+e.getMessage(), e);
//		}
//		List<BrandVo> oldbrandVoList = catBrandService.getBrand(catVo.getCatNo());
//		map.put("brandVoList", brandVoList);
//		map.put("oldbrandVoList", oldbrandVoList);
		map.put("catVo", catlist.get(0));
		return "product/update_commodity_cat";
	}
	
	
	/**
     * @Title: updateCommodityCat
     * @Description: 修改分类
     * @param catVo
     * @param map
     * @return
     * @author: gaoyaxiong
     * @date: 2013-5-27下午01:31:46
     */
	@RequestMapping("updateCommodityCat")
	@ResponseBody
	public String updateCommodityCat(CommodityCatVo catVo,String[] brandNO){
		return commodityCatService.updateCommodityCat(catVo,brandNO);
	}
	
	/**
     * @Title: delectCommodityCat
     * @Description: 删除分类
     * @param catVo
     * @param map
     * @return
     * @author: gaoyaxiong
     * @date: 2013-5-27下午01:31:46
     */
	@RequestMapping("delectCommodityCat")
	@ResponseBody
	public String delectCommodityCat(CommodityCatVo catVo,ModelMap map){
		String ts = commodityCatService.delectCommodityCat(catVo);
		return ts;
	}
	
	/**
     * @Title: getCommodityCatShow
     * @Description: 查看分类页面
     * @param catVo
     * @param map
     * @return
     * @author: gaoyaxiong
     * @date: 2013-5-27下午01:31:46
     */
	@RequestMapping("getCommodityCatShow")
	public String getCommodityCatShow(CommodityCatVo catVo,ModelMap map){
		List<CommodityCatVo> catlist = new ArrayList<CommodityCatVo>();
		if(StringUtil.isExist(catVo.getCatNo())){
			catlist = commodityCatService.getCommodityCatList(catVo);
		}
//		List<BrandVo> brandVoList = catBrandService.getBrand(catVo.getCatNo());
//		map.put("brandVoList", brandVoList);
		map.put("catVo", catlist.get(0));
		return "product/commodity_cat_show";
	}
	
	/**
     * @Title: getCommodityCatByNo
     * @Description: 查看编码是否存在
     * @param catVo
     * @param map
     * @return
     * @author: gaoyaxiong
     * @date: 2013-5-27下午01:31:46
     */
	@RequestMapping("getCommodityCatByNo")
	@ResponseBody
	public String getCommodityCatByNo(String no){
		return commodityCatService.getCommodityCatByNo(no);
	}
	
	 /**
     * 验证分类名称是否重复
     * @throws Exception 
     * 
     * */
    @ResponseBody
    @RequestMapping("checkCatName")
    public String checkCatName(String catName) throws Exception
    {
    	if(null!= catName && LogicUtil.isNotNullAndEmpty(catName)){
    		List<CommodityCat> list = commodityCatService.checkCatName(catName);
    		if(null != list && list.size()>0){
    			return SZSCMConstant.FAIL;
    		}
    	}
         return SZSCMConstant.SUCCESS;
    }
	
}
